<?php

class NavCompanyController extends BaseController
{
	
	/**
	 * 商家中心菜单管理
	 */
	public function actionAdmin()
	{
		$model=new Nav('search');
		$model->unsetAttributes();  // clear any default values
		$model->zna_type = 1;//'类型：1商家中心，2会员中心，3前台页面'
		if(isset($_GET['Nav']))
			$model->attributes=$_GET['Nav'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * 保存
	 */
	 public function actionSave(){
	 	$nav = $_POST['Nav'];
	 	foreach($nav['zna_listorder'] as $key=>$val){
	 		$model = Nav::model()->findByPk($key);
	 		$model->zna_listorder = $val;
	 		$model->zna_url = $nav['zna_url'][$key];
	 		$model->save();
	 	}
	 	//成功跳转
		$this->redirect(Yii::app()->request->urlReferrer);
	 }
	 
	 /**
	  * 切换显示隐藏
	  */
	 public function actionChangeStatus(){
	 	$id = (int)trim($_GET['id']);
	 	$model=$this->loadModel($id);
	 	$model->zna_status = ($_GET['status']=='1')?2:1;
	 	$model->save();
	 	//成功跳转
		$this->redirect(Yii::app()->request->urlReferrer);
	 }
	 
	 /**
	  * 新建菜单
	  */
	 public function actionCreate()
	 {
	 	$model=new Nav;
	 	if(isset($_GET['pid'])){
	 		$model->zna_parentid = (int)$_GET['pid'];
	 	}
	 	$model->zna_type = 1;//'类型：1商家中心，2会员中心，3前台页面'
	 	if(isset($_POST['Nav']))
	 	{
	 		$model->attributes=$_POST['Nav'];
	 		if($model->save())
	 			$this->redirect(array('admin'));
	 	}
	 
	 	$this->render('create',array(
	 			'model'=>$model,
	 	));
	 }
	 
	 /**
	  *修改菜单
	  */
	 public function actionUpdate($id)
	 {
	 	$model=$this->loadModel($id);
	 
	 	if(isset($_POST['Nav']))
	 	{
	 		$model->attributes=$_POST['Nav'];
	 		if($model->save())
	 			$this->redirect(array('admin'));
	 	}
	 
	 	$this->render('update',array(
	 			'model'=>$model,
	 	));
	 }
	 
	 /**
	  * 删除菜单
	  * @param integer $id 菜单ID
	  */
	 public function actionDelete($id)
	 {
	 	// 删除操作
	 	$criteria= new CDbCriteria;
	 	$criteria->addInCondition('ip_id', $_POST['selectdel']);
	 	$this->deleteAll($criteria);
	 	//成功跳转
	 	$this->redirect(Yii::app()->request->urlReferrer);
	 }
	 
	 /**
	  * 加载模型
	  */
	 public function loadModel($id)
	 {
	 	$model=Nav::model()->findByPk($id);
	 	if($model===null)
	 		throw new CHttpException(404,'The requested page does not exist.');
	 	return $model;
	 }
	 
}